package inits

import (
	"context"
	"fmt"
	"food-mgr/food-service/configs"
	"github.com/go-redis/redis/v8"
	"sync"
)

var (
	Ctx = context.Background()
	Rdb *redis.Client
)

func Redis() {

	var once sync.Once
	once.Do(func() {
		RedisData := configs.Config.Redis
		Addr := fmt.Sprintf("%s:%d", RedisData.Host, RedisData.Port)
		Rdb = redis.NewClient(&redis.Options{
			Addr:     Addr,
			Password: RedisData.Password, // no password set
			DB:       RedisData.Database, // use default DB
		})

		err = Rdb.Set(Ctx, "key", "value", 0).Err()
		if err != nil {
			panic("redis连接失败")
		} else {
			fmt.Println("redis连接成功")
		}
	})

}
